package pwr.online.cow;

import pwr.online.cow.model.Board;
import pwr.online.cow.model.Position;

public abstract class CowSearchAlgorithm implements CowPositionUpdateListener {

	protected Board mBoard;
	protected Position mCowPosition;
	
	public CowSearchAlgorithm(Board board, Position cowPosition) {
		mBoard = board;
		mCowPosition = cowPosition;
		mBoard.addHistoryEntry(getInitialPosition());
	}
	
	public void setCowPosition(Position cowPosition) {
		mCowPosition = cowPosition;
	}
	
	boolean doStep() {
		if (mBoard.getHistoryCount() != 0 &&
	        mCowPosition.equals(mBoard.getHistoryAtTime(mBoard.getHistoryCount()-1))) {
			return true;
		}
		mBoard.addHistoryEntry(getNewPosition());
		return false;
	}
	
	abstract public Position getNewPosition();
	abstract public Position getInitialPosition();
	abstract public void reset();

	@Override
	public void CowPositionUpdated(Position position) {
		mCowPosition = position;	
	}
}
